<?php
namespace Home\Controller;

use Common\Controller\HomebaseController;
use Common\Model\MerchantModel;
use Common\Model\MerchantTypeModel;


class BrandController extends HomebaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    //品牌
    public function index()
    {
        $merchant_name = I('merchant_name');
        $sort_name = I('sort_name');

        if (!$sort_name) {
            $sort_name = 1;
        }

        $where = array(
            'use_flg' => 1
        );
        $order = array();


        if ($merchant_name) {
            $where['name'] = array('like', "%$merchant_name%");
        }

        if ($sort_name == 1) {
            $order = array('area' => 'ASC');
        } else if ($sort_name == 2) {
            $order = array('name' => 'ASC');
        } else {
            $order = array();
        }


        $merchantModel = new MerchantModel();

        $typeModel = new MerchantTypeModel();
        $merchants = $merchantModel->where($where)->order($order)->select();


        foreach ($merchants as &$merchant) {
            $id_array = explode(',', $merchant['type_id']);
            $type_array = array();
            foreach ($id_array as $id) {
                $type = $typeModel->where(array('id' => $id))->find();
                $type_array[] = $type['name'];
            }
            $merchant['type_name_str'] = implode(' / ', $type_array);

        }

        $this->assign('merchant_name', $merchant_name);
        $this->assign('sort_name', $sort_name);

        $this->assign('merchants', $merchants);
        $this->display();
    }

    //门店地图
    public function brandMap()
    {

    }
}
